﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBManager
{
    public class DBManager
    {
        private GPSDataClassesDataContext m_db = null;
        static DBManager instance = null;

        static public string connectionString;

        DBManager()
        {
            m_db = new GPSDataClassesDataContext();
           
        }

        public static DBManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DBManager();
                }

                return instance;
            }
        }

        public void Add(GPSLog gpsLog)
        {
            m_db.GPSLogs.InsertOnSubmit(gpsLog);

        }

        public void Add(GeoLocation geo)
        {
            m_db.GeoLocations.InsertOnSubmit(geo);
        }

        public GeoLocation FindGeo(decimal lat,decimal lng)
        {
           List<GeoLocation> list =  m_db.GeoLocations.Where<GeoLocation>(d => Math.Abs((decimal)(d.lat - lat)) < (decimal)0.01 && Math.Abs((decimal)(d.@long - lng)) < (decimal)0.01).ToList<GeoLocation>();

           if (list.Count == 0)
               return null;

           return list.First();
        }

        public List<GPSTracker> GetAllTracker()
        {
            //m_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, m_db.GPSLogs);
            m_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, m_db.GPSTrackers);

            return m_db.GPSTrackers.Where<GPSTracker>(d => d.trackerId != 0).ToList<GPSTracker>();
        }

        public GPSTracker GetTracker(int trakerNo)
        {
           
           // m_db.Refresh(System.Data.Linq.RefreshMode.KeepChanges, m_db.GPSTrackers);

            return m_db.GPSTrackers.SingleOrDefault(d => (int)d.trackerNo == trakerNo);

        }

        public GPSLog GetLatestLog(int trackerNo)
        {
            m_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, m_db.GPSTrackers);

            GPSTracker tracker =  m_db.GPSTrackers.SingleOrDefault(d => (int)d.trackerNo == trackerNo);

            if (tracker == null)
                return null;

            return tracker.GPSLogs.Last<GPSLog>();
        }
        public List<GPSLog> GetLatestOneDay(GPSTracker tracker)
        {
            m_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, m_db.GPSTrackers);
           // m_db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, m_db.GPSLogs);


            return tracker.GPSLogs.Where<GPSLog>(d => (int)d.gpstrakerId == tracker.trackerId && d.time >= DateTime.Today && d.time <= DateTime.Today.AddDays(1)).ToList<GPSLog>();
        }

        public void DeleteOlderLog()
        {
            m_db.GPSLogs.DeleteAllOnSubmit<GPSLog>(m_db.GPSLogs.Where<GPSLog>(d => (DateTime.Today - (DateTime)d.time).TotalDays >= 90));
        }

        //Persistence
        public void Save()
        {
            m_db.SubmitChanges();
        }

    }
}
